package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.AttractionImgs;
import com.qzct.entity.po.AttractionSave;
import com.qzct.entity.vo.AttractionVo;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.AttractionImgsService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.Attractions;
import com.qzct.service.AttractionsService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-11
 */
@RestController
@Api(tags = "景点信息")
@RequestMapping("/attractions")
public class AttractionsController {

    @Autowired
    private AttractionsService attractionsService;
    @Autowired
    private AttractionImgsService attractionImgsService;

    /**
     * 添加。
     *
     * @param attractions 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("添加风景")
    public boolean save(@RequestBody @ApiParam("") Attractions attractions) {
        return attractionsService.save(attractions);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键删除")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return attractionsService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param attractions 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") Attractions attractions) {
        return attractionsService.updateById(attractions);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<Attractions> list() {
        return attractionsService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主健查询景点详情")
    public ResultVo getInfo(@PathVariable @ApiParam("主键") int id) {
        AttractionVo attractionVo = new AttractionVo();
        attractionVo.setAttractions(attractionsService.getById(id));
        attractionVo.setList(attractionImgsService.getByCid(id));
        return new ResultVo(attractionVo);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<Attractions> page(@ApiParam("分页信息") Page<Attractions> page) {
        return attractionsService.page(page);
    }



    /**
     * 添加。
     *
     * @param attractions
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("saves")
    @ApiOperation("添加风景")
    public ResultVo save(@RequestBody @ApiParam("") AttractionSave attractionSave) {
        attractionsService.insert(attractionSave.getAttractions());
        int id = attractionsService.getMaxId();
        for (int i = 0; i < attractionSave.getUrls().size(); i++) {
            AttractionImgs attractionImgs = new AttractionImgs();
            attractionImgs.setUrl(attractionSave.getUrls().get(i));
            attractionImgs.setAttractionId(id);
            attractionImgsService.save(attractionImgs);
        }
        return ResultVo.success("添加成功");
    }

//    热门景点(女)
    @GetMapping("hot0")
    @ApiOperation("热门景点女")
    public List<AttractionVo> list0() {
        ArrayList<AttractionVo> list = new ArrayList<>();
        ArrayList<Integer> integers = attractionsService.gethot0();
        for (int i = 0; i < integers.size(); i++) {
            AttractionVo attractionVo = new AttractionVo();
            attractionVo.setAttractions(attractionsService.getById(integers.get(i)));
            attractionVo.setList(attractionImgsService.getByCid(integers.get(i)));
            list.add(attractionVo);
        }

        return list;
    }
    //    热门景点(男)
    @GetMapping("hot1")
    @ApiOperation("热门景点男")
    public List<AttractionVo> list1() {
        ArrayList<AttractionVo> list = new ArrayList<>();
        ArrayList<Integer> integers = attractionsService.gethot1();
        for (int i = 0; i < integers.size(); i++) {
            AttractionVo attractionVo = new AttractionVo();
            attractionVo.setAttractions(attractionsService.getById(integers.get(i)));
            attractionVo.setList(attractionImgsService.getByCid(integers.get(i)));
            list.add(attractionVo);
        }
        return list;
    }
    //    热门景点
    @GetMapping("hot")
    @ApiOperation("热门景点")
    public List<AttractionVo> hot() {
        ArrayList<AttractionVo> list = new ArrayList<>();
        ArrayList<Integer> hot = attractionsService.getHot();
        for (int i = 0; i < hot.size(); i++) {
            AttractionVo attractionVo = new AttractionVo();
            attractionVo.setAttractions(attractionsService.getById(hot.get(i)));
            attractionVo.setList(attractionImgsService.getByCid(hot.get(i)));
            list.add(attractionVo);
        }
        return list;
    }

    @GetMapping("like/{id}")
    @ApiOperation("点赞")
    public ResultVo like(@PathVariable @ApiParam("主键") Serializable id) {
        attractionsService.addlike(id);
        return ResultVo.success("成功");
    }
    @GetMapping("all")
    @ApiOperation("查看全部")
    public ResultVo all() {
        ArrayList<Attractions> list = attractionsService.getList();
        ArrayList<AttractionVo> vos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            AttractionVo attractionVo = new AttractionVo();
            attractionVo.setAttractions(list.get(i));
            attractionVo.setList(attractionImgsService.getByCid(list.get(i).getAttractionId()));
            vos.add(attractionVo);
        }
        return ResultVo.success("成功",vos);
    }
}
